
const webpack = require('webpack');
const CompressionWebpackPlugin = require('compression-webpack-plugin')
module.exports = {
    chainWebpack: (config) => {
        // if (config.plugins instanceof Array) {
        //     // 开启 BundleAnalyzerPlugin
        //     config.plugins.push(new BundleAnalyzerPlugin());
        // }
        // 未来应该是run serve 不要运行这一行

        let imageLoader = {
            test: /\.(gif|jpg|png|jpe?g|svg)$/i,
            use: [{
                loader: 'image-webpack-loader',
                options: {
                    mozjpeg: {
                        progressive: true,
                    },
                    // optipng.enabled: false will disable optipng
                    optipng: {
                        enabled: false,
                    },
                    pngquant: {
                        quality: [0.65, 0.90],
                        speed: 4
                    },
                    gifsicle: {
                        interlaced: false,
                    },
                    // the webp option will enable WEBP
                    webp: {
                        quality: 75
                    }
                }
            }]
        };
        config.plugin('ContextReplacementPlugin')
            .use(new webpack.ContextReplacementPlugin(
                /moment[/\\]locale$/,
                /zh-cn/));

        config.plugin('CompressionWebpackPlugin')
            .use(new CompressionWebpackPlugin({
                algorithm: 'gzip',
                test: /\.js$|\.html$|\.css/, // 匹配的文件名
                threshold: 10240, // 文件超过10k，进行gzip压缩
                minRatio: 0.8,
                deleteOriginalAssets: false // 是否删除原文件
            }));

        config.module
            .rule('min-image')
            .test(/\.(png|jpe?g|gif)(\?.*)?$/)
            .use('image-webpack-loader')
            .loader('image-webpack-loader');


        // config
        //     .plugin('webpack-bundle-analyzer')
        //     .use(require('webpack-bundle-analyzer').BundleAnalyzerPlugin)



    }
}
